/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */
class Solution {
    public boolean isCompleteTree(TreeNode root) {
        Queue<TreeNode> queue = new LinkedList<>();
        boolean flag = false;
        if(root == null){
            return true;
        }
        queue.offer(root);
        while(!queue.isEmpty()){
            TreeNode curNode = queue.poll();
            // 1.结点存在右子树而无左子树，return false
            // 2.遇到第一个左右结点不双全的结点，接下来遇到的都应为叶子结点
            if((flag && (curNode.left != null || curNode.right != null))
                || (curNode.left == null && curNode.right != null)){
                return false;
            }
            if(curNode.left != null){
                queue.offer(curNode.left);
            }
            if(curNode.right != null){
                queue.offer(curNode.right);
            }
            if(curNode.left == null || curNode.right == null){
                flag = true;
            }
        }
        return true;
    }
}